package main

import (
  "bytes";
  "flag";
  "fmt";
  "json";
  "io/ioutil";
  "os";
  _ "strings";
  "strconv";
  "graph";
)

var configFlag = flag.String("c", "data.json", "Config file name");
var helpFlag = flag.Bool("h", false, "This help");

func main() {
  flag.Parse();
  
  if *helpFlag {
    flag.PrintDefaults();
    os.Exit(graph.EXIT_SUCCESS);
  }
  
  configJsonBytes, err := ioutil.ReadFile(*configFlag);
  if err != nil {
    fmt.Fprintf(os.Stderr, "failed to read %s: %s\n", *configFlag, err.String());
    os.Exit(graph.EXIT_NO_CONFIG);
  }
  configJson := bytes.NewBuffer(configJsonBytes).String();

  //var config = Config{};
//  ok, errtok := json.Unmarshal(configJson, &config);
  
  graphBuilder := graph.NewConfBuilder();
  graphBuilder.Init();
  ok, errindx, errtok := json.Parse(configJson, graphBuilder);
  if !ok {
    fmt.Fprintf(os.Stderr, "Config error at %s (while reading %s)(index %d)\n", strconv.Quote(errtok), *configFlag, errindx);
    os.Exit(graph.EXIT_CONFIG_PARSE);
  }
  fmt.Printf("%#v",graphBuilder);
  g := graphBuilder.Graphs.At(6).(*graph.Graph);
  g.DoBfs("v2");
  fmt.Printf("%s", g.GetBfsResults());
}
